# -*- coding: utf-8 -*-
# @Time :    2022/6/18 18:27
# @Author :  mac
# @File :    iphone_tidevice
# @Project : appium
# @Description: 不使用xcode启动wda,使用tidevice启动 wda
"""
1.通过xcode给手机安装webDriverAgent
2.手机--设置--通用--描述文件--信任
3.查看连接设备 tidevice list
4.查看WDA的bundleId,命令：tidevice applist
5.启动wda，命令：tidevice -u [设备 udid] wdaproxy -B [wda 的 bundle Id] --port 8100 成功提示 wda start successfully
example:tidevice wdaproxy -B com.facebook.WebDriverAgentRunner20220613.xctrunner --port 8100
6.测试打开手机设置 tidevice launch com.apple.Preferences
7.配置appium参数,使用appium做自动化

  "platformName": "ios",
  "platformVersion": "14.6",
  "deviceName": "iPhone11",
  "udid": "00008101-000255021E08001E",
  "bundleId": "被测应用的boundleid",
  "webDriverAgentUrl": "http://127.0.0.1:8100",
  "noReset": true,
  "usePrebuiltWDA": false,
  "useXctestrunFile": false,
  "skipLogCapture": true,
  "automationName": "XCUITest"
}

8.开启appium server
9.start session     webdriver.Remote('http://127.0.0.1:4723', caps)
"""
import yaml
from appium import webdriver


def desired():
    # 读取YAML配置文件
    with open('../Data/caps_tidevice.yaml', 'r', encoding='utf-8') as file:
        data = yaml.full_load(file)  # yaml 5.1新用法

    # 初始化配置数据
    caps = dict()
    caps['platformName'] = data['platformName']
    caps['platformVersion'] = data['platformVersion']
    caps['deviceName'] = data['deviceName']
    caps['udid'] = data['udid']
    caps['bundleId'] = data['bundleId']
    caps['webDriverAgentUrl'] = data['webDriverAgentUrl']
    caps['noReset'] = data['noReset']                     # false,清除session信息；true不清除session信息
    caps['usePrebuiltWDA'] = data['usePrebuiltWDA']
    caps['useXctestrunFile'] = data['useXctestrunFile']
    caps['skipLogCapture'] = data['skipLogCapture']
    caps['automationName'] = data['automationName']
    caps['app'] = data['app']  # 自动安装APP
    caps['autoAcceptAlerts'] = data['autoAcceptAlerts']  # 自动同意隐私授权
    caps['unicodeKeyboard'] = data['unicodeKeyboard']
    caps['resetKeyboard'] = data['resetKeyboard']

    # 启动APP
    driver = webdriver.Remote('http://'+str(data['ip'])+':'+str(data['port'])+'/wd/hub', caps)
    driver.implicitly_wait(12)
    return driver


if __name__ == '__main__':
    desired()
